package cn.iocoder.yudao.module.crm.controller.admin.receipttitle;

import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactRespVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.contact.CrmContactDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.crm.controller.admin.receipttitle.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.receipttitle.ReceiptTitleDO;
import cn.iocoder.yudao.module.crm.service.receipttitle.ReceiptTitleService;

@Tag(name = "管理后台 - crm发票抬头")
@RestController
@RequestMapping("/crm/receipt-title")
@Validated
public class ReceiptTitleController {

    @Resource
    private ReceiptTitleService receiptTitleService;

    @PostMapping("/create")
    @Operation(summary = "创建crm发票抬头")
    @PreAuthorize("@ss.hasPermission('crm:receipt-title:create')")
    public CommonResult<Long> createReceiptTitle(@Valid @RequestBody ReceiptTitleSaveReqVO createReqVO) {
        return success(receiptTitleService.createReceiptTitle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新crm发票抬头")
    @PreAuthorize("@ss.hasPermission('crm:receipt-title:update')")
    public CommonResult<Boolean> updateReceiptTitle(@Valid @RequestBody ReceiptTitleSaveReqVO updateReqVO) {
        receiptTitleService.updateReceiptTitle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除crm发票抬头")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:receipt-title:delete')")
    public CommonResult<Boolean> deleteReceiptTitle(@RequestParam("id") Long id) {
        receiptTitleService.deleteReceiptTitle(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除crm发票抬头")
                @PreAuthorize("@ss.hasPermission('crm:receipt-title:delete')")
    public CommonResult<Boolean> deleteReceiptTitleList(@RequestParam("ids") List<Long> ids) {
        receiptTitleService.deleteReceiptTitleListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得crm发票抬头")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:receipt-title:query')")
    public CommonResult<ReceiptTitleRespVO> getReceiptTitle(@RequestParam("id") Long id) {
        ReceiptTitleDO receiptTitle = receiptTitleService.getReceiptTitle(id);
        return success(BeanUtils.toBean(receiptTitle, ReceiptTitleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得crm发票抬头分页")
    @PreAuthorize("@ss.hasPermission('crm:receipt-title:query')")
    public CommonResult<PageResult<ReceiptTitleRespVO>> getReceiptTitlePage(@Valid ReceiptTitlePageReqVO pageReqVO) {
        PageResult<ReceiptTitleDO> pageResult = receiptTitleService.getReceiptTitlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReceiptTitleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出crm发票抬头 Excel")
    @PreAuthorize("@ss.hasPermission('crm:receipt-title:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReceiptTitleExcel(@Valid ReceiptTitlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReceiptTitleDO> list = receiptTitleService.getReceiptTitlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "crm发票抬头.xls", "数据", ReceiptTitleRespVO.class,
                        BeanUtils.toBean(list, ReceiptTitleRespVO.class));
    }

    @GetMapping("/simple-all-list")
    @Operation(summary = "获得发票抬头的精简列表")
    @PreAuthorize("@ss.hasPermission('crm:receipt-title:query')")
    public CommonResult<List<ReceiptTitleRespVO>> getSimpleContactList(@RequestParam("customerId") Long customerId) {
        List<ReceiptTitleDO> list = receiptTitleService.getReceiptTitleList(getLoginUserId(), customerId);
        return success(convertList(list, receiptTitle -> // 只返回 id、name 字段
                new ReceiptTitleRespVO().setId(receiptTitle.getId()).setTitle(receiptTitle.getTitle())));
    }

}